package com.eystar.pdc.util;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.eystar.ext.kit.ContainerKit;
import com.eystar.ext.kit.tree.BaseTree;
import com.eystar.pdc.model.Fav;

/**
 * 此类是为了显示右侧菜单栏，如果要菜单栏选中，需要在Controller中index代码中添加如下信息
 * setAttr(Constant.MENU_REMARK_NAME, getPara(Constant.MENU_REMARK_NAME));
 * 
 * 这一块还是有问题的，就是如果是页面中的某个按钮点击刷新时，就不能选中了。。。。。。。。。。。。。以后修改，这个可以用全局过滤器来实现
 * 
 * 该问题于2016年7月9日解决，在AuthInterceptor中解决的
 * @author auth
 *
 */
public class MenuHelper {

	/**
	 * 输出顶部菜单
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String outTopMenu(HttpServletRequest request){
		String path = request.getContextPath();
		StringBuffer html = new StringBuffer();
		html.append("<ul class='nav navbar-nav'>");
		for (BaseTree tree : (List<BaseTree>) ContainerKit.get("resource_tree")) {
			if (!isVisiable(request, tree)) {
				continue;
			}
			List<BaseTree> subResources = tree.getChildren();
			if (subResources != null && !subResources.isEmpty()) { // 如果有子菜单
				html.append("<li class='dropdown-hover #" + tree.getId() + "'>");
					html.append("<a href='javascript:void(0)' class='dropdown-toggle'>");
						html.append("<i class='ace-icon " + tree.getAttr("res_icon") + " bigger-160'></i>");
						html.append("&nbsp;&nbsp;").append(tree.getNodeName()).append("&nbsp;&nbsp;");
						html.append("<i class='arrow fa fa-angle-down'></i>");
					html.append("</a>");
				StringBuffer subHtml = new StringBuffer();
				loopTopMenu(subResources,tree,subHtml,request);
				html.append(subHtml);
				html.append("</li>");
			}else { // 如果没有子菜单
				String url = path + "/" + tree.getAttr("res_url");
				html.append("<li class='#" + tree.getId() + "'>");
					html.append("<a class='menuItem' res_id='"+tree.getId()+"' data-id='"+url+"' href='"+url
							+ "' target='" + (((Integer)tree.getAttr("res_target")) == 10 ? "_self" : "_blank") + "'>");
						html.append("<i class='ace-icon " + tree.getAttr("res_icon") + " bigger-160'></i>");
						html.append("&nbsp;&nbsp;").append(tree.getNodeName()).append("&nbsp;&nbsp;");
					html.append("</a>");
				html.append("</li>");
			}
		}
		html.append("</ul>");
		return html.toString();
	}
	
	/**
	 * 如果根节点有子菜单，进入此循环
	 * @param subResources
	 * @param parentResource
	 * @param subHtml
	 * @param request
	 */
	public static void loopTopMenu(List<BaseTree> subResources,BaseTree parentResource,StringBuffer subHtml,HttpServletRequest request){
		String path = request.getContextPath();
		subHtml.append("<ul class='dropdown-menu dropdown-light-blue'>");
		for (BaseTree subResource : subResources) {
			if (!isVisiable(request, subResource)) {
				continue;
			}
			// 如果有子菜单
			if (subResource.getChildren() != null && !subResource.getChildren().isEmpty() && parentResource.getId().equals(subResource.getParentId())) {
				subHtml.append("<li class='dropdown-hover #" + subResource.getId() + "'>");
					subHtml.append("<a href='javascript:void(0)' class='dropdown-toggle'>");
						subHtml.append("<i class='ace-icon "+subResource.getAttr("res_icon")+" bigger-110 blue'></i>");
						subHtml.append("&nbsp;&nbsp;").append(subResource.getNodeName()).append("&nbsp;&nbsp;");
						subHtml.append("<i class='arrow fa fa-caret-right'></i>");
					subHtml.append("</a>");
					loopTopMenu(subResource.getChildren(),subResource,subHtml,request);
				subHtml.append("</li>");
			}else { // 如果没有子菜单
				String url = path + "/" + subResource.getAttr("res_url");
				subHtml.append("<li class='#" + subResource.getId() + "'>");
					subHtml.append("<a class='menuItem' res_id='"+subResource.getId()+"' data-id='"+url+"' href='"+url
							+ "' target='" + (((Integer)subResource.getAttr("res_target")) == 10 ? "_self" : "_blank") + "'>");
						subHtml.append("<i class='ace-icon " + subResource.getAttr("res_icon") + " bigger-110 blue'></i>");
						subHtml.append("&nbsp;&nbsp;").append(subResource.getNodeName()).append("&nbsp;&nbsp;");
					subHtml.append("</a>");
				subHtml.append("</li>");
			}
		}
		subHtml.append("</ul>");
	}
	
	/**
	 * 输出左侧菜单---左侧改为收藏菜单了
	 * @param request
	 * @return
	 */
	public static String outNavMenu(HttpServletRequest request) {
		Fav fav = new Fav();
		fav.setOrPut("create_user", PdcHelper.getCurrentUserName(request));
		List<Fav> favs = Fav.dao.findFavs(fav);
		StringBuffer html = new StringBuffer();
		for (Fav fav2 : favs) {
			String url = fav2.getStr("url");
			html.append("<li>");
				html.append("<a class='menuItem' res_id='"+fav2.getStr("res_id")+"' data-id='"+url+"' href='"+url
						+ "' target='" + ((fav2.getInt("res_target") != null && fav2.getInt("res_target") == 10) ? "_self" : "_blank") + "'>");
					html.append("<i class='ace-menu-icon "+fav2.getStr("res_icon")+"'></i>");
					html.append("<span class='ace-menu-text'>&nbsp;&nbsp;"+fav2.getStr("name")+"&nbsp;&nbsp;</span>");
				html.append("</a>");
			html.append("</li>");
		}
		return html.toString();
	}

	/**
	 * 判定该资源是否可见
	 * @param request
	 * @param tree
	 * @return
	 */
	public static boolean isVisiable(HttpServletRequest request,BaseTree tree){
		if ((Integer)tree.getAttr("status") == LoginUtil.DISPLAY_STATUS && PdcHelper.hasAuth(request, tree.getAttr("res_remark")+"_look")) {
			return true;
		}
		return false;
	}
}
